//
//  ZDBaseCollectionViewController.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/13.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDBaseCollectionViewController.h"
#import <MJRefresh/MJRefresh.h>
#import "ZDNoDataView.h"

@interface ZDBaseCollectionViewController ()

@property (nonatomic,strong)MJRefreshNormalHeader *header;

// <#售票员01#>
@property(nonatomic,strong)MJRefreshAutoFooter *footer;

/**
 没有数据的 view
 */
@property(nonatomic,strong)ZDNoDataView *noDataView;

@end

@implementation ZDBaseCollectionViewController
- (void)setupUI
{
    [super setupUI];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //1.表格
    [self.view addSubview:self.collectionView];
    
    //2.没有数据的 view
    ZDNoDataView *noDataView = [[ZDNoDataView alloc]initWithFrame: CGRectMake(0, 200, LEScreenWidth, 146)];
    [self.collectionView addSubview:noDataView];
    noDataView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    noDataView.hidden = YES;
    self.noDataView = noDataView;
}
- (void)loadData
{
    
}
- (void)initHeader
{
    ZDLog(@"%s---%d---初始化刷新头部",__func__,__LINE__);
    __weak typeof (self) weakSelf = self;
    self.header  = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 1;
        [weakSelf requestRefreshData];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int16_t)(0.2*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.header endRefreshing];
        });
    }];
    [self.header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
    [self.header setTitle:@"松开刷新" forState:MJRefreshStatePulling];
    [self.header setTitle:@"正在刷新" forState:MJRefreshStateRefreshing];
    self.collectionView.mj_header = self.header;
}
- (void)initFooter
{
    ZDLog(@"%s---%d---初始化刷新的脚视图",__func__,__LINE__);
    __weak typeof(self) weakSelf = self;
    self.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [weakSelf.header endRefreshing];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int16_t)(0.1*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.footer endRefreshing];
        });
        weakSelf.page++;
        if (weakSelf.page > [self.totalPage integerValue]) {
            [SVProgressHUD showInfoWithStatus:@"没有更多了"];
            weakSelf.page--;
            return ;
        }
        [self requestRefreshData];
        
    }];
    self.collectionView.mj_footer = self.footer;
    
}
- (void)requestRefreshData;
{
    
}
- (void)resetCollectionViewFrame:(CGRect)frame
{
    self.collectionView.frame = frame;
//    [self.collectionView layoutIfNeeded]; 
}
- (void)addNoDataViewWithTipText:(NSString *)tipText
{
    self.noDataView.hidden = NO;
    [self.noDataView setNoDataText:tipText];
    
}
- (void)hiddenNoDataView
{
    self.noDataView.hidden = YES;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 0;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"" forIndexPath:indexPath];
    return  cell;
}

#pragma mark- getter
- (UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat itemWith = (LEScreenWidth-3)/4;
        layout.itemSize = CGSizeMake(itemWith, itemWith);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        
        CGRect rect = CGRectMake(0,0, LEScreenWidth, LEScreenHeight - 64);
        _collectionView = [[UICollectionView alloc]initWithFrame:rect collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
    }
    return _collectionView;
}
- (NSMutableArray *)dataSource
{
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
@end
